<?php

namespace common\observer;

use support\SingletonTrait;
use common\model\ProjectModel;
use common\service\OrderService;
use common\service\StatusService;
use common\service\CommonService;
use teamones\dbeventObserver\interfaces\SplSubject;
use teamones\dbeventObserver\interfaces\SplObserver;

/**
 * 外包任务任务状态同步到甲方任务
 */
class OutsideTaskSyncDataToPrimaryProject implements SplObserver
{
    use SingletonTrait;

    protected $syncFields = [
        'task_status',
        'end_time',
        'start_time',
        'delivery_time',
        'plan_end_time',
        'plan_delivery_time'
    ];

    /**
     * @inheritDoc
     */
    function update(SplSubject $subject): void
    {
        $updateData = $subject->getSplUpdateData();
        $changedFields = array_keys($updateData);
        if (!array_intersect($changedFields, $this->syncFields)) {
            return;
        }
        $syncData = [];
        foreach ($updateData as $updateKey => $updateValue) {
            if (!in_array($updateKey, $this->syncFields)) {
                continue;
            }
            $syncData[$updateKey] = $updateValue;
        }
        if (empty($syncData)) {
            return;
        }


        if (isset($updateData['task_status'])) {
            $cancelStatus = StatusService::getInstance()->getTaskDefaultClosedStatus();
            if ($updateData['task_status'] == $cancelStatus['id']) {
                return;
            }
        }

        $task = $subject->getSubjectData();

        if ($task['step_type'] != 'step') {
            return;
        }

        $project = model(ProjectModel::class)->find($task['project_id']);

        if ($project['type'] == 'insource') {
            // 不存在外包任务则返回
            $targetTask = OrderService::getInstance()->getOutsideTaskByTaskId($task['id']);
            if (empty($targetTask)) {
                return;
            }
            $structure = [];
            if (isset($syncData['plan_end_time'])) {
                $structure['plan_end_time'] = 'date,Y-m-d H:i:s';
            }
            if (isset($syncData['plan_delivery_time'])) {
                $structure['plan_delivery_time'] = 'intval';
            }
            // 计划时间没有变动的不同步
            if (empty($structure)) {
                return;
            }
            $syncData['id'] = $targetTask['id'];
            $syncData = query_array_construct(array_merge(['id' => 'intval'], $structure), $syncData);
        } else if ($project['type'] == 'outsource') {
            $sourceTask = OrderService::getInstance()->getSourceTaskByTaskId($task['id']);
            if (empty($sourceTask)) {
                return;
            }
            if (isset($syncData['plan_end_time'])) {
                $syncData['plan_end_time'] = date('Y-m-d H:i:s', $syncData['plan_end_time']);
            }
            $syncData['id'] = $sourceTask['id'];
        } else {
            return;
        }
        // 修改任务状态
        try {
            CommonService::instance('task')
                ->setCurrentModuleCode('task')
                ->update($syncData, 'task', false);
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }

}
